package net.chathu.airgw.model 
{
	import flash.utils.Dictionary;
	import net.chathu.airgw.model.RoomList;
	import net.chathu.airgw.model.UserList;
	/**
	 * ...
	 * @author ze
	 */
	public class ChatData 
	{
		
		public var isModerator:Boolean;
		//szabalysertes miatt csak a zartosztaly szobaba tud belepni?
		public var isLimited:Boolean;
		public var isRegistered:Boolean;
		public var username:String;
		public var userId:int;
		public var chatOptions:ChatOptions;
		
		public var users:UserList;
		public var rooms:RoomList;
		public var joinedRooms:Vector.<JoinedRoomInfo>;
		public var privateChats:Vector.<PrivateChatInfo>;
		public var privateMessageQueue:PrivateMessageQueue;
		public var isInUsersRoom:Boolean = false;
		
		public var friendList:Dictionary; //baratok listaja
		public var ignoreList:Dictionary; //nemitottak listaja
		
		public function ChatData() 
		{
			users = new UserList();
			rooms = new RoomList();
			joinedRooms = new Vector.<JoinedRoomInfo>();
			privateChats = new Vector.<PrivateChatInfo>();
			privateMessageQueue = new PrivateMessageQueue();
			chatOptions = new ChatOptions();
			friendList = new Dictionary();
			ignoreList = new Dictionary();
		}
		
				
		public function isInRoom(room:Room):Boolean
		{
			for (var i:int = 0; i < joinedRooms.length; i++)
			{
				var roomInfo:JoinedRoomInfo = JoinedRoomInfo(joinedRooms[i]);
				if (roomInfo.roomId == room.id)
				{
					//ha bennt vagyunk a szobaban, akkor van rola infonk
					return true;
				}
			}
			return false;
		}
		
		public function removeJoinedRoomInfo(roomId:int):Boolean {
			
			trace("items before remove:" + joinedRooms.length);
			for (var i:int = joinedRooms.length - 1; i >= 0; i--)
			{
				var roomInfo:JoinedRoomInfo = JoinedRoomInfo(joinedRooms[i]);
				if (roomInfo.roomId == roomId)
				{
					joinedRooms.splice(i, 1);
					trace("items after remove:" + joinedRooms.length);
					
					return true; //csak 1 lehet bent..
				}
			}
			return false;
		}
		
		
		public function getJoinedRoomInfo(roomId:int):JoinedRoomInfo
		{
			for (var i:int = 0; i < joinedRooms.length; i++)
			{
				var roomInfo:JoinedRoomInfo = JoinedRoomInfo(joinedRooms[i]);
				if (roomInfo.roomId == roomId)
				{
					return roomInfo;
				}
			}
			return null;
		}
		
		//azokat a szobakat(JoinedRoomInfo tomb) adja vissza, ahol mindketten bent vagytok
		public function getCommonJoinedRoomsWithUser(userId:int):Array {
			var ret:Array = new Array();
			for each(var joinedRoom:JoinedRoomInfo in joinedRooms)
			{
				if (joinedRoom.checkUserById(userId))
				{
					ret.push(joinedRoom);
				}
			}
			
			return ret;
		}
		
		
		public function getPrivateChatInfo(privateId:int):PrivateChatInfo
		{
			for (var i:int = 0; i < privateChats.length; i++)
			{
				var privInfo:PrivateChatInfo = PrivateChatInfo(privateChats[i]);
				if (privInfo.privateId == privateId)
				{
					return privInfo;
				}
			}
			return null;
		}
		
		public function getPrivateChatInfoByPartnerId(partnerId:int):PrivateChatInfo
		{
			for (var i:int = 0; i < privateChats.length; i++)
			{
				var privInfo:PrivateChatInfo = PrivateChatInfo(privateChats[i]);
				if (privInfo.partnerId == partnerId)
				{
					return privInfo;
				}
			}
			return null;
		}
		
		public function getPrivateChatInfoByCloseId(closeId:int):PrivateChatInfo
		{
			for (var i:int = 0; i < privateChats.length; i++)
			{
				var privInfo:PrivateChatInfo = PrivateChatInfo(privateChats[i]);
				if (privInfo.closeId == closeId)
				{
					return privInfo;
				}
			}
			return null;
		}
		
		
		public function removePrivateChatInfo(privateId:int):Boolean {
			
			for (var i:int = privateChats.length - 1; i >= 0; i--)
			{
				var privInfo:PrivateChatInfo = PrivateChatInfo(privateChats[i]);
				if (privInfo.privateId == privateId)
				{
					privateChats.splice(i, 1);
					return true; //csak 1 lehet bent..
				}
			}
			return false;
		}
		
		public function addFriend(regId:int, userName:String):void {
			friendList[regId] = userName;
		}
		
		public function removeFriend(regId:int):String {
			var ret:String = friendList[regId];
			delete friendList[regId];
			return ret;
		}
		
		public function isFriend(regId:int):Boolean {
			return typeof friendList[regId] != "undefined";
		}
		
		public function addIgnore(regId:int, userName:String):void {
			ignoreList[regId] = userName;
		}
		
		public function removeIgnore(regId:int):String {
			var ret:String = ignoreList[regId];
			delete ignoreList[regId];
			return ret;
		}
		
		public function isIgnored(regId:int):Boolean {
			return typeof ignoreList[regId] != "undefined";
		}
		
		
	}

}